package com.ymatou.iapi.dguser.testcase;

import com.ymatou.iapi.dguser.parameter.UnionIdInfoBean;
import com.ymatou.iapi.dguser.parameter.UserBindDto;
import com.ymatou.iapi.dguser.parameter.UserCreateBean;
import com.ymatou.iapi.dguser.service.UnionIdInfoCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.DGUserCallService;
import com.ymttest.database.model.DGThirdPartyBindInfo;
import com.ymttest.database.sqlwapper.DGUserWapper;
import com.ymttest.utils.TestDataManager;
import org.junit.*;
import org.junit.experimental.categories.Category;

import java.util.List;

/**
*获取unionId绑定列表
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_UnionIdInfo{
	private static UnionIdInfoBean unionidinfoBean;
	private static UnionIdInfoCall unionidinfoCall;
	DGUserWapper DGUserWapper;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("获取unionId绑定列表");
	}

	@Before
	public void caseUp() {
		unionidinfoBean=new UnionIdInfoBean();
		unionidinfoCall=new UnionIdInfoCall();
		DGUserWapper = new DGUserWapper();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(unionidinfoCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	private void verifyDB(String unionid) {
		List<DGThirdPartyBindInfo> dgThirdPartyBindInfos = DGUserWapper.selectDGThirdPartyByUnionId(unionid);
		List<UserBindDto> userBindDtos = unionidinfoCall.getUserBindDtos();
		for (UserBindDto userBindDto:userBindDtos
			 ) {
			dgThirdPartyBindInfos.forEach(p->{
				if (userBindDto.getUserId() == p.getUserId()){
					Logger.verifyEquals(userBindDto.getAppSource(),p.getAppSource(),"验证appSource");
					Logger.verifyEquals(userBindDto.getBindId(),p.getBindId(),"验证BindId");
					Logger.verifyEquals(userBindDto.getOpenId(),p.getOpenId(),"验证openId");
					Logger.verifyEquals(userBindDto.getUserId(),p.getUserId(),"验证userid");
					Logger.verifyEquals(userBindDto.getThirdPartyType(),p.getThirdPartyType(),"验证ThirdPartyType");
				}
			});
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_UnionIdInfo_001() {
		Logger.start(true,"获取unionId绑定列表，一个unionId绑定一个openId");
		try {
			UserCreateBean usercreateBean= DGUserCallService.initUsercreateBean();
			usercreateBean.setMobile(TestDataManager.getPhoneNum());
			DGUserCallService.UserCreateCall(usercreateBean);
			unionidinfoBean.setUnionId(usercreateBean.getUnionId());
			unionidinfoBean.setBelongSellerId(0);
			unionidinfoBean.setUserType(0);
			unionidinfoCall.setData(unionidinfoBean);
			unionidinfoCall.callService();
			Logger.verifyEquals("true", unionidinfoCall.getString("success"), "验证success");
			verifyDB(unionidinfoBean.getUnionId());
		} catch (Exception e) {
			Logger.fail(e);
		}catch (Throwable throwable) {
			throwable.printStackTrace();
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_UnionIdInfo_002() {
		Logger.start(true,"获取unionId绑定列表，一个unionId绑定多个openId");
		try {
			//买家
			UserCreateBean usercreateBean= DGUserCallService.initUsercreateBean();
			DGUserCallService.UserCreateCall(usercreateBean);
			//商家
			UserCreateBean usercreateBean1= DGUserCallService.initUsercreateBean();
			usercreateBean1.setMobile(TestDataManager.getRandomNum(11));
			usercreateBean1.setMobileAreaCode(TestDataManager.getRandomNum(6));
			usercreateBean1.setAppSource(60);
			usercreateBean1.setUserType(1);
			usercreateBean1.setUnionId(usercreateBean.getUnionId());
			DGUserCallService.UserCreateCall(usercreateBean1);

			unionidinfoBean.setUnionId(usercreateBean.getUnionId());
			unionidinfoBean.setBelongSellerId(0);
			unionidinfoBean.setUserType(0);
			unionidinfoCall.setData(unionidinfoBean);
			unionidinfoCall.callService();
			Logger.verifyEquals("true", unionidinfoCall.getString("success"), "验证success");
			verifyDB(unionidinfoBean.getUnionId());

		} catch (Exception e) {
			Logger.fail(e);
		}catch (Throwable throwable) {
			throwable.printStackTrace();
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_UnionIdInfo_003() {
		Logger.start(true,"获取unionId绑定列表，不传unionId");
		try {
			unionidinfoBean.setUnionId(null);
			unionidinfoBean.setBelongSellerId(0);
			unionidinfoBean.setUserType(0);
			unionidinfoCall.setData(unionidinfoBean);
			unionidinfoCall.callService();
			Logger.verifyEquals("false", unionidinfoCall.getString("success"), "验证success");
			Logger.verifyEquals("unionId:unionId must", unionidinfoCall.getString("errorMessage"), "验证errorMessage");
		} catch (Exception e) {
			Logger.fail(e);
		}catch (Throwable throwable) {
			throwable.printStackTrace();
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_UnionIdInfo_005() {
		Logger.start(true,"获取unionId绑定列表，不输入BelongSellerId失败");
		try {
			UserCreateBean usercreateBean= DGUserCallService.initUsercreateBean();
			usercreateBean.setMobile(TestDataManager.getPhoneNum());
			DGUserCallService.UserCreateCall(usercreateBean);
			unionidinfoBean.setUnionId(usercreateBean.getUnionId());
//			unionidinfoBean.setBelongSellerId(0);
			unionidinfoBean.setUserType(0);
			unionidinfoCall.setData(unionidinfoBean);
			unionidinfoCall.callService();
			Logger.verifyEquals("false", unionidinfoCall.getString("success"), "验证success");
		} catch (Exception e) {
			Logger.fail(e);
		}catch (Throwable throwable) {
			throwable.printStackTrace();
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_UnionIdInfo_006() {
		Logger.start(true,"获取unionId绑定列表，不输入UserType失败");
		try {
			UserCreateBean usercreateBean= DGUserCallService.initUsercreateBean();
			usercreateBean.setMobile(TestDataManager.getPhoneNum());
			DGUserCallService.UserCreateCall(usercreateBean);
			unionidinfoBean.setUnionId(usercreateBean.getUnionId());
			unionidinfoBean.setBelongSellerId(0);
//			unionidinfoBean.setUserType(0);
			unionidinfoCall.setData(unionidinfoBean);
			unionidinfoCall.callService();
			Logger.verifyEquals("false", unionidinfoCall.getString("success"), "验证success");
		} catch (Exception e) {
			Logger.fail(e);
		}catch (Throwable throwable) {
			throwable.printStackTrace();
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_UnionIdInfo_007() {
		Logger.start(true,"获取unionId绑定列表，一个unionId绑定一个openId,usetype1");
		try {
			UserCreateBean usercreateBean= DGUserCallService.initSellerUsercreateBean();
			DGUserCallService.UserCreateCall(usercreateBean);
			unionidinfoBean.setUnionId(usercreateBean.getUnionId());
			unionidinfoBean.setBelongSellerId(0);
			unionidinfoBean.setUserType(1);
			unionidinfoCall.setData(unionidinfoBean);
			unionidinfoCall.callService();
			Logger.verifyEquals("true", unionidinfoCall.getString("success"), "验证success");
			verifyDB(unionidinfoBean.getUnionId());
		} catch (Exception e) {
			Logger.fail(e);
		}catch (Throwable throwable) {
			throwable.printStackTrace();
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_UnionIdInfo_008() {
		Logger.start(true,"获取unionId绑定列表，一个unionId绑定一个openId,独立小程序belongsellerid不等于0");
		try {
			UserCreateBean usercreateBean= DGUserCallService.initUsercreateBean();
			usercreateBean.setBelongSellerId(DGUserCallService.TestWxsqOtherSellerId);
			DGUserCallService.UserCreateCall(usercreateBean);
			unionidinfoBean.setUnionId(usercreateBean.getUnionId());
			unionidinfoBean.setBelongSellerId(DGUserCallService.TestWxsqOtherSellerId);
			unionidinfoBean.setUserType(0);
			unionidinfoCall.setData(unionidinfoBean);
			unionidinfoCall.callService();
			Logger.verifyEquals("true", unionidinfoCall.getString("success"), "验证success");
			verifyDB(unionidinfoBean.getUnionId());
		} catch (Exception e) {
			Logger.fail(e);
		}catch (Throwable throwable) {
			throwable.printStackTrace();
		}
	}

}